package com.portlet.controller;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.portlet.model.bophan;
import com.portlet.model.nhanvien;
import com.portlet.model.role;
import com.portlet.service.bophanLocalServiceUtil;
import com.portlet.service.nhanvienLocalServiceUtil;
import com.portlet.service.roleLocalServiceUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

public class quanlyBP extends MVCPortlet {
	public void addBP(ActionRequest actionRequest, ActionResponse actionResponse)
			throws SystemException {
		String maBP = actionRequest.getParameter("maBP");
		String tenBP = actionRequest.getParameter("tenBP");
		String ghiChu = actionRequest.getParameter("GhiChu");

		bophan bp = bophanLocalServiceUtil.createbophan(maBP);
		bp.setTenBoPhan(tenBP);
		bp.setGhiChu(ghiChu);
		bophanLocalServiceUtil.addbophan(bp);

		System.out.println("Them thanh cong bo phan");
		actionResponse.setRenderParameter("JspPage", "/view.jsp");
	}
	public void addRL(ActionRequest actionRequest, ActionResponse actionResponse) throws SystemException{
		String maRL=actionRequest.getParameter("maRL");
		String tenRL=actionRequest.getParameter("tenRL");
		String ghiChu=actionRequest.getParameter("ghiChu");
		
		role ro=roleLocalServiceUtil.createrole(maRL);
		ro.setTenRole(tenRL);
		ro.setGhiChu(ghiChu);
		roleLocalServiceUtil.addrole(ro);
		System.out.println("Them vi tri thanh cong");
		actionResponse.setRenderParameter("jspPage", "/viewRole.jsp");
	}
	
	public static String convertDateToString(Date date, String format) {
	    SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
	    return mySimpleDateFormat.format(date);
	}
	 
	public static java.util.Date convertStringToDate(String dateStr, String format) {
	    SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
	    try {
	        return mySimpleDateFormat.parse(dateStr);
	    } catch (ParseException e) {
	        return null;
	    }
	}
	
	
	public void addNV(ActionRequest actionRequest, ActionResponse actionResponse) throws SystemException{
		String maNV =actionRequest.getParameter("maNV");
		String tenNV = actionRequest.getParameter("tenNV");
		String gioitinh =actionRequest.getParameter("gioitinh");
		String ngaysinh=actionRequest.getParameter("ngaysinh");
		String maBP=actionRequest.getParameter("maBP");
		String maRL=actionRequest.getParameter("maRL");
		
		Date ns=convertStringToDate(ngaysinh, "yyyy-MM-dd");
		nhanvien nv=nhanvienLocalServiceUtil.createnhanvien(maNV);
		nv.setTenNhanVien(tenNV);
		if(gioitinh.equals("nu")){
			nv.setGioiTinh("0");
		}else{
			nv.setGioiTinh("1");
		}
		
		nv.setNgaySinh(ns);
		nv.setMaBoPhan(maBP);
		nv.setMaRole(maRL);
		nhanvienLocalServiceUtil.addnhanvien(nv);
		System.out.println("Them nhan vien thanh cong");
		actionResponse.setRenderParameter("jspPage", "/viewNV.jsp");
	}
	
	public void editBP(ActionRequest actionRequest,
			ActionResponse actionResponse) throws PortalException,
			SystemException {
		String maBP = actionRequest.getParameter("maBP");
		bophan bp = bophanLocalServiceUtil.getbophan(maBP);

		System.out.println(bp.toString());
		actionRequest.setAttribute("edit", bp);
		actionResponse.setRenderParameter("jspPage", "/edit.jsp");
	}

	public void editRL(ActionRequest actionRequest, ActionResponse actionResponse) throws PortalException, SystemException{
		String maRL=actionRequest.getParameter("maRL");
		role ro=roleLocalServiceUtil.getrole(maRL);
		System.out.println(ro.toString());
		actionRequest.setAttribute("edit1", ro);
		actionResponse.setRenderParameter("jspPage", "/editRL.jsp");
	}
	
	public void editNV(ActionRequest actionRequest,ActionResponse actionResponse) throws PortalException, SystemException{
		String maNV=actionRequest.getParameter("maNV");
		nhanvien nv=nhanvienLocalServiceUtil.getnhanvien(maNV);
		System.out.println(nv.toString());
		actionRequest.setAttribute("edit", nv);
		actionResponse.setRenderParameter("jspPage", "/editNV.jsp");
	}
	public void updatenameURL(ActionRequest actionRequest,
			ActionResponse actionResponse) throws PortalException,
			SystemException {
		String maBP = actionRequest.getParameter("maBP");
		String tenBP = actionRequest.getParameter("tenBP");
		String ghiChu = actionRequest.getParameter("ghiChu");

		bophan nl = bophanLocalServiceUtil.getbophan(maBP);
		nl.setTenBoPhan(tenBP);
		nl.setGhiChu(ghiChu);
		bophanLocalServiceUtil.updatebophan(nl);

		System.out.println("Cap nhat thanh cong");
		actionResponse.setRenderParameter("jspPage", "/view.jsp");
	}
	public void updatenameRLURL(ActionRequest actionRequest, 
				ActionResponse actionResponse) throws PortalException, SystemException{
		String maRL=actionRequest.getParameter("maRL");
		String tenRL=actionRequest.getParameter("tenRL");
		String ghiChu=actionRequest.getParameter("ghiChu");
		
		role ro=roleLocalServiceUtil.getrole(maRL);
		ro.setTenRole(tenRL);
		ro.setGhiChu(ghiChu);
		roleLocalServiceUtil.updaterole(ro);
		System.out.println("Cap nhat thanh cong");
		actionResponse.setRenderParameter("jspPage", "/viewRole.jsp");
	}
	public void updatenameNVURL(ActionRequest actionRequest, ActionResponse actionResponse) throws PortalException, SystemException{
		String maNV =actionRequest.getParameter("maNV");
		String tenNV = actionRequest.getParameter("tenNV");
		String gioiTinh=actionRequest.getParameter("gioiTinh");
		String ngaySinh=actionRequest.getParameter("ngaySinh");
		String maBP=actionRequest.getParameter("maBP");
		String maRole=actionRequest.getParameter("maRole");
		
		Date ns=convertStringToDate(ngaySinh,"yyyy-MM-dd" );
		nhanvien nv=nhanvienLocalServiceUtil.getnhanvien(maNV);
		nv.setTenNhanVien(tenNV);
		nv.setGioiTinh(gioiTinh);
		nv.setNgaySinh(ns);
		nv.setMaBoPhan(maBP);
		nv.setMaRole(maRole);
		nhanvienLocalServiceUtil.updatenhanvien(nv);
		
		System.out.println("Cap nhat thanh cong nhan vien");
		actionResponse.setRenderParameter("jspPage", "/viewNV.jsp");
		
	}
	public void deleteBP(ActionRequest actionRequest,
				ActionResponse actionResponse) throws PortalException, SystemException{
		String maBP=actionRequest.getParameter("edit");
		bophan nl=bophanLocalServiceUtil.getbophan(maBP);
		bophanLocalServiceUtil.deletebophan(nl);
		
		System.out.println("Xoa thanh cong");
		actionResponse.setRenderParameter("jspPage", "/view.jsp");
	}
	public void deleteRL(ActionRequest actionRequest, ActionResponse actionResponse) throws PortalException, SystemException{
		String maRL=actionRequest.getParameter("edit");
		role ro=roleLocalServiceUtil.getrole(maRL);
		
		roleLocalServiceUtil.deleterole(ro);
		System.out.println("Xoa thanh cong");
		actionResponse.setRenderParameter("jspPage", "/viewRole.jsp");
	}
	public void deleteNV(ActionRequest actionRequest, ActionResponse actionResponse) throws SystemException{
		String maNV=actionRequest.getParameter("edit");
		nhanvien nv=nhanvienLocalServiceUtil.createnhanvien(maNV);
		
		nhanvienLocalServiceUtil.deletenhanvien(nv);
		System.out.println("Xoa nhan vien thanh cong");
		actionResponse.setRenderParameter("jspPage", "/viewNV.jsp");
	}
}

